/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.imagesteganographylibrary.Utils;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.ResourceManager;
import ohos.global.resource.WrongTypeException;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import java.io.IOException;


/**
 * 资源工具类
 *
 * @author:hello
 * @since 2021-04-25
 */
public class ResUtils {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(0, 0, "ResUtils");
    private static final int CONSTANT = 160;

    private ResUtils() {
    }

    /**
     * vp转px
     *
     * @param context 上下文
     * @param vp vp
     * @return px
     */
    public static int vpToPx(Context context, float vp) {
        return (context.getResourceManager().getDeviceCapability().screenDensity / CONSTANT * Math.round(vp));
    }

    /**
     * px转vp
     *
     * @param context 上下文
     * @param px px
     * @return vp
     */
    public static int pxTovp(Context context, float px) {
        return (Math.round(px) / (context.getResourceManager().getDeviceCapability().screenDensity / CONSTANT));
    }

    /**
     * 获取屏幕的宽
     *
     * @param context 上下文
     * @return 屏幕的宽，单位px
     */
    public static int getScreenWidth(Context context) {
        return vpToPx(context, context.getResourceManager().getDeviceCapability().width);
    }

    /**
     * 获取屏幕的高
     *
     * @param context 上下文
     * @return 屏幕的高，单位px
     */
    public static int getScreenHeight(Context context) {
        return vpToPx(context, context.getResourceManager().getDeviceCapability().height);
    }

    /**
     * get string
     *
     * @param context the context
     * @param id      the string id
     * @return string of the given id
     */
    public static String getString(Context context, int id) {
        String result = "";
        if (context == null) {
            HiLog.error(LABEL_LOG, "getString -> get null context");
            return result;
        }
        ResourceManager manager = context.getResourceManager();
        if (manager == null) {
            HiLog.error(LABEL_LOG, "getString -> get null ResourceManager");
            return result;
        }
        try {
            result = manager.getElement(id).getString();
        } catch (IOException e) {
            HiLog.error(LABEL_LOG,
                    "getString -> IOException");
        } catch (NotExistException e) {
            HiLog.error(LABEL_LOG,
                    "getString -> NotExistException");
        } catch (WrongTypeException e) {
            HiLog.error(LABEL_LOG,
                    "getString -> WrongTypeException");
        }
        return result;
    }

    /**
     * get the string array
     *
     * @param context the context
     * @param id      the string array id
     * @return the string array
     */
    public static String[] getStringArray(Context context, int id) {
        String[] result = null;
        if (context == null) {
            HiLog.error(LABEL_LOG,
                    "getStringArray -> get null context");
            return result;
        }
        ResourceManager manager = context.getResourceManager();
        if (manager == null) {
            HiLog.error(LABEL_LOG,
                    "getStringArray -> get null ResourceManager");
            return result;
        }
        try {
            result = manager.getElement(id).getStringArray();
        } catch (IOException e) {
            HiLog.error(LABEL_LOG,
                    "getStringArray -> IOException");
        } catch (NotExistException e) {
            HiLog.error(LABEL_LOG,
                    "getStringArray -> NotExistException");
        } catch (WrongTypeException e) {
            HiLog.error(LABEL_LOG,
                    "getStringArray -> WrongTypeException");
        }
        return result;
    }

    /**
     * get the int
     *
     * @param context the context
     * @param id      the int array
     * @return the int array
     */
    public static int getInt(Context context, int id) {
        int result = 0;
        if (context == null) {
            HiLog.error(LABEL_LOG,
                    "getInt -> get null context");
            return result;
        }
        ResourceManager manager = context.getResourceManager();
        if (manager == null) {
            HiLog.error(LABEL_LOG,
                    "getInt -> get null ResourceManager");
            return result;
        }
        try {
            result = manager.getElement(id).getInteger();
        } catch (IOException e) {
            HiLog.error(LABEL_LOG,
                    "getInt -> IOException");
        } catch (NotExistException e) {
            HiLog.error(LABEL_LOG,
                    "getInt -> NotExistException");
        } catch (WrongTypeException e) {
            HiLog.error(LABEL_LOG,
                    "getInt -> WrongTypeException");
        }
        return result;
    }

    /**
     * 颜色值rgba转换为ShapeElement
     *
     * @param rgbaColor 颜色值
     * @return ShapeElement
     */
    public static ShapeElement getShapeElement8(int rgbaColor) {
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(new RgbColor(rgbaColor));
        return shapeElement;
    }
}
